package cn.usian.controller;


import cn.usian.common.R;
import cn.usian.entity.Employee;
import cn.usian.service.EmpService;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/employee")
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping("{id}")
    public R findById(@PathVariable Long id){
        return empService.findById(id);
    }

    @PutMapping
    public R updateEmp(@RequestBody Employee employee,HttpSession session){
        long id=(long) session.getAttribute("employee");
        employee.setUpdateUser(id);
        empService.updateEmp(employee);
        return R.success("修改成功");
    }
    @PostMapping
    public R save(@RequestBody Employee employee,HttpSession session){
        Long id = (long)session.getAttribute("employee");
        employee.setCreateUser(id);
        employee.setUpdateUser(id);
        empService.save(employee);
        return R.success("添加成功");
    }

    @PostMapping("/logout")
    public  R logout(HttpSession session){
        session.invalidate();
        return R.success("退出成功");
    }

    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpSession session){
        R<Employee> r = empService.login(employee);
        //1：成功
        if(r.getCode()==1){
            //将用户的编号保存在session中
            session.setAttribute("employee",r.getData().getId());
        }
        return r;
    }

    @GetMapping("page")
    public R<Page> page(int page, int pageSize, String name){
        return empService.page(page,pageSize,name);
    }



}
